package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.Uservice;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
@Slf4j
@RequiredArgsConstructor
public class UserController {
    final private Uservice uservice;

    @PostMapping
    @ApiOperation("新增用户")
    public void saveUser(@ApiParam(value = "用户表单实体") @RequestBody UserFormDTO userFormDTO) {
        log.info("新增用户：{}", userFormDTO);
        // 校验UserFormDTO的参数是否合法
        // 1.转换DTO为PO
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        // 2.新增
        uservice.save(user);
    }

    /**
     * 批量新增用户
     * @param userFormDTOList 用户列表
     * @return 批量新增的结果
     * @throws Exception 如果操作失败抛出异常
     */
    @PostMapping("/batch")
    @ApiOperation("批量新增用户")
    public void saveUserBatch(@ApiParam(value = "用户表单实体") @RequestBody List<UserFormDTO> userFormDTOList) {
        log.info("批量新增用户：{}", userFormDTOList);
        // 1.转换DTO为PO
        List<User> users = BeanUtil.copyToList(userFormDTOList, User.class);
        // 2.批量新增
        uservice.saveBatchCheck(users);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    public void deleteUser(@ApiParam(value = "用户id") @PathVariable Long id) {
        log.info("删除用户：{}", id);
        // 1.删除
        uservice.removeById(id);
    }

    @GetMapping("/{id}")
    @ApiOperation("查询用户")
    public UserVO queryUserById(@ApiParam(value = "用户id") @PathVariable Long id) {
        log.info("查询用户：{}", id);
        User user = uservice.getById(id);
        // 2.利用hutool工具转换
        return BeanUtil.copyProperties(user, UserVO.class);
    }

    @GetMapping("/ComplexConditionalQueries")
    @ApiOperation("复杂条件查询用户信息")
    public List<UserVO> complexQueries(@ApiParam(value = "复杂条件查询用户实体")  UserQuery userQuery) {
        log.info("复杂条件查询用户：{}", userQuery);
        List<User> users = uservice.conplexConditionalQueries(userQuery);

        return BeanUtil.copyToList(users, UserVO.class);

    }

    @GetMapping
    @ApiOperation("查询用户列表")
    public List<UserVO> queryUserList(@ApiParam(value = "批量查询用户") @RequestParam List<Long> ids) {
        log.info("批量查询用户：{}", ids);
        List<User> users = uservice.listByIds(ids);
        return BeanUtil.copyToList(users, UserVO.class);
    }

    @PutMapping("/{id}/reduceMoney/{reduceMoney}")
    @ApiOperation("扣减用户余额")
    public void reduceMoney(@ApiParam(value = "用户id") @PathVariable Long id,
                            @ApiParam(value = "减少金额") @PathVariable Integer reduceMoney) {
        log.info("扣减用户id：{},扣减余额：{}", id, reduceMoney);
        uservice.reduceMoney(id, reduceMoney);
    }


    @GetMapping("/address/{id}")
    @ApiOperation("根据用户id查询地址")
    public UserVO queryAddressByUserId(@ApiParam(value = "用户id") @PathVariable Long id) {
        log.info("根据用户id查询地址：{}", id);
        return uservice.queryAddressByUserId(id);
    }
    @GetMapping("/page")
    @ApiOperation("分页查询用户列表")
    public PageDTO<UserVO> queryUserList(@ApiParam(value = "分页查询用户") UserQuery userQuery) {
        log.info("分页查询用户：{}", userQuery);
        return  uservice.queryUserList(userQuery);
    }



}
